JavaScript syntax
part 6/30 Β· 107.0 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Primitive data types
The JavaScript language provides six primitive data types:
β’ Undefined
β’ Number
β’ BigInt
β’ String
β’ Boolean
β’ Symbol
Some of the primitive data types also provide a set of named values that
represent the extents of the type boundaries. These named values are
described within the appropriate sections below.
Undefined
The value of "undefined" is assigned to all uninitialized variables, and
is also returned when checking for object properties that do not exist.
In a Boolean context, the undefined value is considered a false value.
Note: undefined is considered a genuine primitive type. Unless
explicitly converted, the undefined value may behave unexpectedly in
comparison to other types that evaluate to false in a logical context.
let test; // variable declared, but not defined, ...
// ... set to value of undefined
const testObj = {};
console.log(test); // test variable exists, but value not ...
// ... defined, displays undefined
console.log(testObj.myProp); // testObj exists, property does not, ...
// ... displays undefined
console.log(undefined == null); // unenforced type during check,
displays true
console.log(undefined === null); // enforce type during check, displays
false
Note: There is no built-in language literal for undefined. Thus (x ===
undefined) is not a foolproof way to check whether a variable is
undefined, because in versions before ECMAScript 5, it is legal for
someone to write var undefined = "I'm defined now";. A more robust
approach is to compare using (typeof x === 'undefined').
Functions like this will not work as expected:
function isUndefined(x) { let u; return x === u; } // like this...
function isUndefined(x) { return x === void 0; } // ... or that second
one
function isUndefined(x) { return (typeof x) === "undefined"; } // ... or
that third one
Here, calling isUndefined(my_var) raises a ReferenceError if my_var is
an unknown identifier, whereas typeof my_var === 'undefined' does not.
Number
Numbers are represented in binary as IEEE 754 floating point doubles.
Although this format provides an accuracy of nearly 16
significant digits, it cannot always exactly represent real numbers,
including fractions.
This becomes an issue when comparing or formatting numbers. For example:
console.log(0.2 + 0.1 === 0.3); // displays false
console.log(0.94 - 0.01); // displays 0.9299999999999999
As a result, a routine such as the toFixed() method should be used to
round numbers whenever they are formatted for output.
Numbers may be specified in any of these notations:
345; // an "integer", although there is only one numeric type in
JavaScript
34.5; // a floating-point number
3.45e2; // another floating-point, equivalent to 345
0b1011; // a binary integer equal to 11
0o377; // an octal integer equal to 255
0xFF; // a hexadecimal integer equal to 255, digits represented by the
...
// ... letters A-F may be upper or lowercase
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ